package com.liduanwei.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.liduanwei.springboot.entity.SuperEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;

public class ServiceUtil {


    /**
     * 根据 map(通常是请求参数) 构建 Page　对象
     *
     * @param params
     * @return
     */
    public static <T> Page<T> generatePage(Map params) {
        Object pageNum = params.get("pageNum");
        Object pageSize = params.get("pageSize");
        Object orderBy = params.get("orderBy");
        int pn = 1;
        int size = 10;
        String orderByStr = "";
        if (StringUtils.isNotEmpty((CharSequence) pageNum)) {
            pn = Integer.parseInt((String) pageNum);
        }
        if (StringUtils.isNotEmpty((CharSequence) pageSize)) {
            size = Integer.parseInt((String) pageSize);
        }
        if (StringUtils.isNotEmpty((CharSequence) orderBy)) {
            orderByStr = (String) orderBy;
        }
        return new Page<T>(pn, size, orderByStr);
    }


    public static SuperEntity fillDefaultValue(SuperEntity entity) {
        if (entity.getCreateBy() == null) {
            entity.setCreateBy(1L);
        }
        if (entity.getUpdateBy() == null) {
            entity.setUpdateBy(1L);
        }
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
        }
        if (entity.getUpdateTime() == null) {
            entity.setUpdateTime(new Date());
        }
        return entity;
    }
}
